package corejava.extend;

import java.util.Date;
import java.util.GregorianCalendar;

class Employee extends Person{
	
	//子类覆盖父类的成员变量
	//private String name;
	
	private double salary;
	private Date hireDay;
	
	public Employee(String n, double s, int year, int month, int day) {
		super(n);
		//name = n;
		salary = s;
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		hireDay = calendar.getTime();
	}
	
/*	public Employee() {
		System.out.println("默认无参的父类构造器！");
	}*/
	
	public String getDescription() {
		return String.format("an employee with a salary of $%.2f", salary);
	}
	
	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	public Date getHireDay() {
		return hireDay;
	}

	public void raiseSalary(double byPercent) {
		double raise = salary * byPercent / 100;
		salary += raise;
	}
	
	//重写Object的equals方法
	public boolean equals(Object otherObject) {
		// a quick test to see if the objects are identical
		if (this == otherObject)
			return true;

		// must return false if the explicit parameter is null
		if (otherObject == null)
			return false;

		// if the classes don't match, they can't be equal
		if (getClass() != otherObject.getClass())
			return false;

		// now we know otherObject is a non-null Employee
		Employee other = (Employee) otherObject;

		// test whether the fields have identical values
		return name.equals(other.name) && salary == other.salary
				&& hireDay.equals(other.hireDay);
	}
	
	//重写Object的hashCode方法
	public int hashCode() {
		return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13
				* hireDay.hashCode();
	}
	
	//重写超类Object的toString方法
	public String toString() {
		return getClass().getName() + "[name=" + name + ",salary=" + salary
				+ ",hireDay=" + hireDay + "]";
	}

}
